package com.bawei.es.controller;

import com.alibaba.fastjson.JSONObject;
import com.bawei.common.core.domain.R;
import com.bawei.es.domain.IndexInfoResp;
import com.bawei.es.service.EsIndexService;
import com.bawei.es.domain.IndexEntry;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * @author : zhangyu
 * @date 2023/11/13 15:23
 */

@AllArgsConstructor
@RestController
@Slf4j
@Api(tags = "es索引管理")
public class EsIndexController {


    private EsIndexService esService;


    @ApiOperation("创建索引")
    @PostMapping("create")
    public R createIndex(@RequestBody IndexEntry indexEntry) {
        esService.createIndex(indexEntry);
        return R.ok();
    }
    @ApiOperation("删除索引")
    @GetMapping("/delete/{indexName}")
    public R deleteIndex(@PathVariable String indexName) {
        esService.deleteIndex(indexName);
        return R.ok();
    }

    @ApiOperation("判断索引是否存在")
    @GetMapping("isexits/{indexName}")
    public R isExists(@PathVariable String indexName) {
        boolean isExists =esService.isExists(indexName);
        return R.ok(isExists);
    }

    @GetMapping("/get/{indexName}")
    @ApiOperation("获取索引信息")
    public R<IndexInfoResp> getIndex(@PathVariable String indexName) {
        IndexInfoResp  index =  esService.getIndex(indexName);
        log.info("获取索引信息返回值:{}",JSONObject.toJSONString(index));
        return R.ok(index);
    }
    @ApiOperation("更新索引")
    @PostMapping("update")
    public R updateIndex(@RequestBody IndexEntry indexEntry) {
        esService.updateIndex(indexEntry);
        return R.ok();
    }







}
